package com.cn.wanxi.servlet.product;

import com.alibaba.fastjson.JSONObject;
import com.cn.wanxi.model.ProductModel;
import com.cn.wanxi.service.IProductService;
import com.cn.wanxi.service.impl.ProductServiceImpl;
import com.cn.wanxi.util.Upload;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/back/product/update")
public class ProductUpdateServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //        1.处理乱码
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json; charset=utf-8");
        Upload upload = new Upload();
        String result = upload.imgUpload(req);
        String[] results = result.split("@@");
        ProductModel productModel = new ProductModel();

        productModel.setCuisineId("".equals(results[0]) ? 0 : Integer.parseInt(results[0]));
        productModel.setProductName(results[1]);
        productModel.setNormalPrice("".equals(results[2]) ? 0.0F : Float.parseFloat(results[2]));
        productModel.setPrice("".equals(results[3]) ? 0.0F : Float.parseFloat(results[3]));
        productModel.setContent(results[4]);
        productModel.setIsShow("".equals(results[5]) ? 0 : Integer.parseInt(results[5]));
        productModel.setIsRecommend("".equals(results[6]) ? 0 : Integer.parseInt(results[6]));
        productModel.setImgHref(results[7]);
        productModel.setId("".equals(results[8]) ? 0 : Integer.parseInt(results[8]));
        IProductService iProductService = new ProductServiceImpl();
        int count = iProductService.update(productModel);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("count", count);
        resp.getWriter().println(jsonObject);

    }
}
